package com.software_product_construction.backend_software_product_construction.service.impl;


import com.software_product_construction.backend_software_product_construction.dao.LesseeMapper;
import com.software_product_construction.backend_software_product_construction.pojo.Lessee;
import com.software_product_construction.backend_software_product_construction.pojo.User;
import com.software_product_construction.backend_software_product_construction.service.LesseeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * 表示的是租户(Lessee)表服务实现类
 *
 * @author makejava
 * @since 2022-11-03 11:31:14
 */
@Service("lesseeService")
public class LesseeServiceImpl implements LesseeService {


    @Autowired
    private LesseeMapper lesseeMapper;


    /*
     * 模糊查询
     * @param nickname String
     * @param from,to int
     * */
    @Override
    public List<User> getUserList(String lesseeId, String nickName, Integer from, Integer to){
        Integer num = to-from+1;
        List<User> users = lesseeMapper.getUserList(lesseeId,nickName,from-1,num);
        return users;
    }

    /*
     * @param id 用户id
     * @param password 密码
     * @param nickname 用户昵称
     * @param sex 用户性别
     * @return boolean
     * */
    @Override
    public Boolean modifyUserInfo(String id, String password, String nickname, Integer sex){
        return lesseeMapper.modifyUserInfo(id,password,nickname,sex);
    }

    /*
     * 查询某一租户的信息
     * @param Lessee
     * */
    @Override
    public List<Lessee> selectLesseeReverse(Lessee lessee){
        return lesseeMapper.selectLessee(lessee).stream().sorted(Comparator.comparing(Lessee::getHistory).reversed()).collect(Collectors.toList());
    }


    /*
    * 根据一个租户实体获取集合
    * */
    @Override
    public List<Lessee> selectLessee(Lessee lessee) {
        return  lesseeMapper.selectLessee(lessee).stream().sorted(Comparator.comparing(Lessee::getHistory)).collect(Collectors.toList());
    }

    @Override
    public List<Lessee> getLesseePersonalHistory(Integer from, Integer to,String id) {
        int num=to-from+1;

        Lessee lessee =  new Lessee();
        lessee.setId(id);
        //获取到最新的count,将他赋值给histroy
        //找寻当前id的lessee
        Lessee historyLessee = lesseeMapper.selectLessee(lessee).get(0);
        //找寻整个历史list集合
        //重置id只查历史
        lessee.setId(null);
        lessee.setAccount(historyLessee.getAccount());
        List<Lessee> lesseeListAll = lesseeMapper.selectLessee(lessee);
        List<Lessee> lesseeList = lesseeMapper.selectLesseePage(from-1,num,lessee);
        if(lesseeListAll.size() == lesseeList.size()) {
            lesseeList.remove(lesseeList.size()-1);
        }
        return lesseeList;
    }


    /*
     * 模糊查询
     * @param id String
     * */
    @Override
    public Boolean deleteUserById(String id){
        return lesseeMapper.deleteUserById(id)==1?true:false;
    }


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Lessee queryById(String id) {
        return this.lesseeMapper.queryById(id);
    }


    /**
     * 新增数据
     *
     * @param lessee
     * @return int
     */
    public int insert(Lessee lessee){
        return lesseeMapper.insert(lessee);
    }


    /**
     * 新增数据
     *
     * @return 实例对象
     */
    @Override
    public int modify(Map<String,Object> map) {
        String id = (String) map.get("id");
        String password = (String)map.get("password");
        String nickname = (String) map.get("nickname");
        Integer sex = (Integer) map.get("sex");


        Lessee lessee =  new Lessee();
        lessee.setId(id);
        //获取到最新的count,将他赋值给histroy
        //找寻当前id的lessee
        Lessee historyLessee = lesseeMapper.selectLessee(lessee).get(0);
        //找寻整个历史list集合
        //重置id只查历史
        lessee.setId(null);
        lessee.setAccount(historyLessee.getAccount());
        //倒序排序
        List<Lessee> historyLessees = selectLesseeReverse(lessee);
        Lessee lesseeUpdate =  historyLessees.get(0);
        lesseeUpdate.setId(UUID.randomUUID().toString());
        lesseeUpdate.setHistory(historyLessees.size());
        lesseeUpdate.setNickname(nickname);
        lesseeUpdate.setPassword(password);
        lesseeUpdate.setSex(sex);
        return lesseeMapper.insert(lesseeUpdate);
    }




}
